package com.profgj.profgj.validater;

import com.profgj.profgj.utils.EmptyUtils;
import com.profgj.profgj.vo.ProductMonthEnergyconsumptionVo;
import org.springframework.validation.Errors;
import org.springframework.validation.Validator;

/**
 * 产品月能耗校验器
 */
public class ProductMonthEnergyconsumptionVoValidator implements Validator {
    /**
     * 类型校验
     * @param clazz 类对象
     * @return
     */
    @Override
    public boolean supports(Class<?> clazz) {

        // 类型比对
        return clazz.equals(ProductMonthEnergyconsumptionVo.class);
    }

    /**
     * 验证逻辑
     * @param target    目标对象
     * @param errors    错误信息保存对象
     */
    @Override
    public void validate(Object target, Errors errors) {

        // 判断对象是否为空
        if(target == null) {
            errors.rejectValue("",null,"产品对象不能为空");
        }

        // 强制转换
        ProductMonthEnergyconsumptionVo productMonthEnergyconsumptionVo = (ProductMonthEnergyconsumptionVo)target;
        // 判断字段是否为空


        if(!EmptyUtils.isLongEmpty(productMonthEnergyconsumptionVo.getProductMonthElectricity())) {
            errors.rejectValue("product_month_electricity",null,"产品月电消耗量不能为空");
        }
        if(productMonthEnergyconsumptionVo.getProductMonthWater() == null  ) {
            errors.rejectValue("product_month_water",null,"产品月水消耗量不能为空");
        }

        if(productMonthEnergyconsumptionVo.getProductMonthCoal() == null  ) {
            errors.rejectValue("product_month_coal",null,"产品月煤消耗量不能为空");
        }
        if(productMonthEnergyconsumptionVo.getProductMonthNaturalgas() == null  ) {
            errors.rejectValue("product_month_naturalgas",null,"产品月天然气消耗量不能为空");
        }
        if(productMonthEnergyconsumptionVo.getProductMonthFueloil() == null  ) {
            errors.rejectValue("product_month_fueloil",null,"产品月燃料油消耗量不能为空");
        }

        if(productMonthEnergyconsumptionVo.getProductMonthYield() == null  ) {
            errors.rejectValue("product_month_yield",null,"产品月产量（吨）不能为空");
        }

        if(productMonthEnergyconsumptionVo.getProductMonthSalesIncome() == null  ) {
            errors.rejectValue("product_month_sales_income",null,"产品月销售收入（万元）不能为空");
        }


    }
}
